<template>
	<om-addpage style="padding-top: 35px;" :checkedit="checkedit" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				biddingIdCacheKey:'mar-gzx-biddingWork-biddingId-cache',
				generalResponsibleUserIdCacheKey:'mar-gzx-biddingWork-generalResponsibleUserId-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','salaryInstId','salaryInstName'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			getWorkParam(){
				let _that = this
				return {
					busCode:_that.busCode,
					generalResponsibleUserId:_that.$refs.autoform.formdata.generalResponsibleUserId
				}
			},
			clearBiddingId(){
				let _that = this
				_that.$refs.autoform.clear("title1","biddingId")
				_that.$refs.autoform.clear("title1","biddingProjectName")
				_that.$refs.autoform.clear("title1","keyProjectId")
				_that.$refs.autoform.clear("title1","generalResponsibleUserId")
				_that.$refs.autoform.clear("title1","tenderNumber")
				_that.$refs.autoform.clear("title1","biddingProjectShortName")
				_that.$refs.autoform.clear("title1","endTime")
				_that.$refs.autoform.clear("title1","biddingStartDate")
				_that.$refs.autoform.clear("title1","salesClerkId")
				_that.$refs.autoform.clear("title1","salesInstId")
				_that.$refs.autoform.clear("title1","projectPhaseId")
				_that.$refs.autoform.clear("title1","stageStartDate")
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = []
				if(fileId){
					ajaxArr.push({
						url:'bidWorkPlan/getInfo',
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("title1","biddingId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstId: ''
							}),
							url:'bidWorkPlan/getBiddingPager',
							namefield:'biddingCode',
							notefield: "biddingProjectName",
							initload:true,
							searchfield:'q',
							cachename:_that.biddingIdCacheKey
						}
					},null,mainData)
					changeEditor("title1","generalResponsibleUserId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstUserId: ''
							}),
							url:'temployee/getStaffCombogrid',
							namefield:'userPosInstName',
							initload:true,
							searchfield:'q',
							cachename:_that.generalResponsibleUserIdCacheKey
						}
					},null,mainData)
					if (_that.subEditorObj['title2']) {
						changeEditor('title2', 'dataStr', {
							getWorkParam:'getWorkParam',
							titleTemplate: '工作项名称：<%= workName %>',
							subtitle2Template: '计划完成日期：<%= endDate %>',
							subtitle3Template: '负责人：<%= dutyMultiOffName %>'
						}, null, mainData);
					}
					
						if(fileId){
							let fieldArr = getFormFields(mainData)
							fieldArr.push("id")
							for(let index in res[0].value.bidWorkPlan){
								if(fieldArr.includes(index)){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[0].value.bidWorkPlan[index])
									_that.$set(_that.formdata,index,res[0].value.bidWorkPlan[index])
								}
							}
							changeEditor("title1","biddingId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstId: res[0].value.bidWorkPlan.biddingId
									}),
									url:'bidWorkPlan/getBiddingPager',
									namefield:'biddingCode',
									notefield: "biddingProjectName",
									initload:true,
									searchfield:'q',
									cachename:_that.biddingIdCacheKey
								}
							},null,mainData)
							changeEditor("title1","generalResponsibleUserId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstUserId: res[0].value.bidWorkPlan.generalResponsibleUserId
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									initload:true,
									searchfield:'q',
									cachename:_that.generalResponsibleUserIdCacheKey
								}
							},null,mainData)
							if(_that.subEditorObj['title2']){
								let dataList = res[0].value.dataList
								_that.subEditorObj['title2'].workList = dataList
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'dataStr',JSON.stringify(dataList))
								_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList))
								delete res[0].value.dataList
							}
						}
					_that.$nextTick(function(){
						
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				_that.om.formSave("bidWorkPlan/addBidWorkPlan",_that.formdata,_that)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this;
			if(_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
			}
			uni.getStorage({
				key:_that.biddingIdCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.biddingId = d.id
						formdata.biddingCode = d.biddingCode
						formdata.biddingProjectName = d.biddingProjectName
						formdata.keyProjectId = d.isKeyProjectId
						formdata.keyProjectName = d.isKeyProjectName
						formdata.generalResponsibleUserId = d.generalResponsibleUserId
						formdata.generalResponsibleUserName = d.generalResponsibleUserName
						formdata.tenderNumber = d.tenderNumber
						formdata.biddingProjectShortName = d.biddingProjectShortName
						formdata.endTime = d.endTime
						formdata.biddingStartDate = d.biddingStartDate
						formdata.salesClerkId = d.salesClerkId
						formdata.salesClerkName = d.salesClerkName
						formdata.salesInstId = d.salesInstId
						formdata.salesInstName = d.salesInstName
						formdata.projectPhaseId = d.projectPhaseId
						formdata.projectPhaseName = d.projectPhase
						formdata.stageStartDate = d.stageStartDate
						_that.$refs.autoform.changeEditor("title1","biddingId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									firstId: d.id
								}),
								url:'bidWorkPlan/getBiddingPager',
								namefield:'biddingCode',
								notefield: "biddingProjectName",
								initload:true,
								searchfield:'q',
								cachename:_that.biddingIdCacheKey
							}
						})
						uni.removeStorage({
							key:_that.biddingIdCacheKey
						})
					}
				}
			})
			uni.getStorage({
				key:_that.generalResponsibleUserIdCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.generalResponsibleUserId = d.id
						formdata.generalResponsibleUserName = d.username
						
						_that.$refs.autoform.changeEditor("title1","generalResponsibleUserId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									firstId: d.id
								}),
								url:'bidWorkPlan/getBiddingPager',
								namefield:'biddingCode',
								notefield: "biddingProjectName",
								initload:true,
								searchfield:'q',
								cachename:_that.generalResponsibleUserIdCacheKey
							}
						})
						uni.removeStorage({
							key:_that.generalResponsibleUserIdCacheKey
						})
					}
				}
			})
		}
	}
</script>

<style>
</style>
                                                                                                 